//
//  XGNewsDetailViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/3/26.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import WebKit
import MBProgressHUD

class XGNewsDetailViewController: UIViewController
{
    init(newsViewModel: XGNewsViewModel)
    {
        self.newsViewModel = newsViewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 控制器生命周期方法
    
    override func loadView()
    {
        super.loadView()
        setUpUI()
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        navigationItem.title = "新闻详情"
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "分享", style: .plain, target: self, action: #selector(shareAction))
        loadRequest()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        MBProgressHUD.hideHUD()
    }
    
    // MARK: - 私有属性
    
    private lazy var webView: WKWebView = {
        let webView = WKWebView()
        webView.navigationDelegate = self
        webView.scrollView.contentInsetAdjustmentBehavior = .never
        webView.scrollView.contentInset = UIEdgeInsets(top: TodayNews_NavigationBar_Height, left: 0, bottom: 0, right: 0)
        return webView
    }()
    private let newsViewModel: XGNewsViewModel
}

// MARK: - WKNavigationDelegate

extension XGNewsDetailViewController: WKNavigationDelegate
{
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
    {
        MBProgressHUD.showProgressHUD(text: "页面正在加载中...")
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    {
        MBProgressHUD.hideHUD()
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)
    {
        MBProgressHUD.showErrorTips(text: "页面加载失败!")
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            MBProgressHUD.hideHUD()
        }
    }
}

// MARK: - 私有扩展

private extension XGNewsDetailViewController
{
    @objc func shareAction() -> Void
    {
        
        UMSocialUIManager.showShareMenuViewInWindow { [weak self] (platform, _) in
            self?.shareInfoWithPlatform(platform: platform)
        }
    }
    
    func shareInfoWithPlatform(platform: UMSocialPlatformType) -> Void
    {
        let imageUrl = "https://mobile.umeng.com/images/pic/home/social/img-1.png"
        let messageObject = UMSocialMessageObject()
        let webObject = UMShareWebpageObject.shareObject(withTitle: newsViewModel.title, descr: nil, thumImage: imageUrl)
        webObject?.webpageUrl = newsViewModel.newsUrl
        messageObject.shareObject = webObject
        UMSocialManager.default()?.share(to: platform, messageObject: messageObject, currentViewController: self, completion: { (result, error) in
            if error != nil {
                MBProgressHUD.showErrorTips(text: "分享失败!")
                return
            }
        })
    }
    
    // 加载网页
    func loadRequest() -> Void
    {
        guard let newsUrl = newsViewModel.newsUrl,
            let url = URL(string: newsUrl) else {
                return
        }
        
        webView.load(URLRequest(url: url, timeoutInterval: 15.0))
    }
    
    func setUpUI() -> Void
    {
        view.backgroundColor = UIColor.white
        view.addSubview(webView)
        webView.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height - TodayNews_Bottom_Margin)
    }
}
